package com.kitchen.ui.dialog.decorate
{
	import com.kitchen.AbstractButton;
	import com.kitchen.ui.box9grid.OuterBox;
	import com.kitchen.ui.button.CloseButton;
	import com.kitchen.ui.dialog.AbstractDialog;
	import com.kitchen.ui.dialog.DialogHeader;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class DecorateDialog extends AbstractDialog
	{
		private var grid:DecoGrid;
		private var position:int;
		private var buttons:Array;
		
		public function DecorateDialog(main:BitmapData, dialog:BitmapData, shoper:Object=null, shop:Object=null)
		{
			super(main, dialog, shoper, shop);
		}
		
		override public function show(cdata:Object):void{
			this.layout(cdata);
		}
		
		override public function close():void{
			this.dispatchEvent(new Event(AbstractDialog.DIALOG_CLOSE));
		}
		
		override protected function addCloseButton():void{
			var closeButton:CloseButton = new CloseButton(this.mainSkin);
			closeButton.name = 'closebtn';
			closeButton.addEventListener(MouseEvent.CLICK, mclickHandle);
			closeButton.x = this.w - 60;
			closeButton.y = 110;
			this.addChild(closeButton);
		}
		
		override protected function layout(cdata:Object):void{
			this.w = 665;
			this.h = 270;
			this.x = 105;
			this.y = 365;
			var outer:OuterBox = new OuterBox(this.dialogSkin, this.w, this.h-100);
			outer.y = 90;
			this.addChild(outer);
			var head:DecorateHead = new DecorateHead(this.mainSkin);
			head.x = 5;
			head.y = 0;
			this.addChild(head);
			this.addCloseButton();
			var left:LeftButton = new LeftButton(this.mainSkin);
			left.name = 'prev';
			var right:RightButton = new RightButton(this.mainSkin);
			right.name = 'next';
			left.x = 35;
			right.x = 518;
			left.y = right.y = 152;
			left.addEventListener(MouseEvent.CLICK, clickHandler);
			right.addEventListener(MouseEvent.CLICK, clickHandler);
			this.addChild(left);
			this.addChild(right);
			
			var gl:GoodsList = new GoodsList();
			gl.x = 80;
			gl.y = 130;
			this.addChild(gl);
			
			var cx:int = 82;
			this.buttons = new Array();
			var ceilingBtn:NewButton = new NewButton(this.mainSkin);
			ceilingBtn.name = 'b1';
			ceilingBtn.x = cx;
			ceilingBtn.y = 102;
			ceilingBtn.addEventListener(MouseEvent.CLICK, cagteHandler);
			ceilingBtn.setChecked(true);
			this.position = 1;
			this.buttons.push(ceilingBtn);
			this.addChild(ceilingBtn);
			var wallBtn:WallButton = new WallButton(this.mainSkin);
			wallBtn.name = 'b2';
			wallBtn.x = cx+64;
			wallBtn.y = 102;
			wallBtn.addEventListener(MouseEvent.CLICK, cagteHandler);
			this.buttons.push(wallBtn);
			this.addChild(wallBtn);
			var floorBtn:FloorButton = new FloorButton(this.mainSkin);
			floorBtn.name = 'b3';
			floorBtn.x = cx+128;
			floorBtn.y = 102;
			floorBtn.addEventListener(MouseEvent.CLICK, cagteHandler);
			this.buttons.push(floorBtn);
			this.addChild(floorBtn);
			var counterBtn:PropertyButton = new PropertyButton(this.mainSkin);
			counterBtn.name = 'b4';
			counterBtn.x = cx+64*3;
			counterBtn.y = 102;
			counterBtn.addEventListener(MouseEvent.CLICK, cagteHandler);
			this.buttons.push(counterBtn);
			this.addChild(counterBtn);
			var saveBtn:SaveButton = new SaveButton(this.mainSkin);
			var sellBtn:SellButton = new SellButton(this.mainSkin);
			saveBtn.x = sellBtn.x = 565;
			saveBtn.y = 160;
			sellBtn.y = 205;
			this.addChild(sellBtn);
			this.addChild(saveBtn);
			this.grid = new DecoGrid(this.dialogSkin);
			this.grid.x = 88;
			this.grid.y = 140;
			this.addChild(this.grid);
			this.grid.goPage(1,1);
		}
		
		private function clickHandler(event:MouseEvent):void{
			var iden:String = event.target.name;
			if(iden == 'prev'){
				this.grid.prevPage(this.position);
			}else if(iden == 'next'){
				this.grid.nextPage(this.position);
			}
				
		}
		
		private function cagteHandler(event:MouseEvent):void{
			var cate:String = String(event.target.name);
			trace(cate);
			var pt:int = int(cate.substr(1,1));
			if(this.position == pt){
				return;
			}
			for each (var ab:AbstractButton in this.buttons){
				if(ab.name != cate){
					ab.setChecked(false);
				}else{
					ab.setChecked(true);
				}
			}
			this.position = pt;
			this.grid.goPage(1, this.position);
		}
	}
}